package com.woniu.darryringmall.front.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.darryringmall.front.entity.District;
import com.woniu.darryringmall.front.service.DistrictService;
import com.woniu.darryringmall.utils.ResponseData;
import com.woniu.darryringmall.utils.ResultStateCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("district")
public class DistrictController {

    @Autowired
    private DistrictService districtService;

    @GetMapping("province")
    public ResponseData province(){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("pid",0);
        List<District> province = districtService.list(wrapper);
        ResponseData responseData = new ResponseData(ResultStateCode.SUCCESS,"");
        responseData.setData(province);
        return responseData;
    }


    /**
     * 返回市区地址
     * @param id
     * @return
     */
    @GetMapping("city/{id}")
    public List city(@PathVariable("id") int id){

        QueryWrapper<District> wrapper = new QueryWrapper();
        wrapper.eq("pid",id);
        List<District> list = districtService.list(wrapper);
        return list;
    }


    @GetMapping("area/{id}")
    public List area(@PathVariable("id") int id){

        QueryWrapper<District> wrapper = new QueryWrapper();
        wrapper.in(true,"pid",id,id+1);
        List<District> list = districtService.list(wrapper);
        return list;
    }
}
